package util

import javax.swing._
import scala.actors.Actor
import scala.actors.Actor._
import java.io.File

object Dialogs{

  /**
   * Shows a dialog which asks for the user's name and password.
   *
   * @return optional (username, password) -pair.
   */
  def askUsernameAndPassword(): Option[Pair[String, String]] = {
    val prompt = new JPanel()
    val labelUser = new JLabel("Käyttäjätunnus:")
    val labelPassword = new JLabel("Salasana:")
    val user = new JTextField(10)
    val pwd = new JPasswordField(10)
    prompt.add(labelUser);
    prompt.add(user);
    prompt.add(labelPassword);
    prompt.add(pwd);

    val action = JOptionPane.showConfirmDialog(null, prompt, "Anna Aamulehden tunnuksesi", JOptionPane.OK_CANCEL_OPTION)

    if (action == JOptionPane.OK_OPTION)
      return Some(user.getText(), pwd.getText())
    else
      return None
  }

  def acroreadNotFound(file: File){
    JOptionPane.showConfirmDialog(null, "Lehteä ei voitu avata, sillä Acroreaderia ei löytynyt. Aamulehti on tallennettu tiedostoon: " + file.getAbsolutePath, "Lehti haettu", JOptionPane.CLOSED_OPTION)
  }

  def loadingDialog(): LoaderDialog = {
    val pane = new JOptionPane("Ladataan lehti, odota hetki.", JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION, null, Array[java.lang.Object]("Keskeytä"))
    val dialog = pane.createDialog("Ladataan lehteä")
    new LoaderDialog(pane, dialog)
  }
}

class LoaderDialog(optionPane: JOptionPane, dialog: JDialog){
  def dispose(){
    dialog.dispose()
  }

  def show(){
    dialog.setVisible(true)
  }
  
  def userCanceledOperation(): Boolean = {
    JOptionPane.UNINITIALIZED_VALUE != optionPane.getValue
  }

  def message(pageNumber: Int){
    optionPane.setMessage("Ladataan sivua: " + pageNumber)
  }
}
